package org.example.utils;

import com.aspose.words.Document;
import com.aspose.words.License;
import org.apache.pdfbox.io.IOUtils;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * @author liyishan
 * @date 2024/3/12 14:49
 * @apiNote
 */
public class PdfUtils {
    private static final Logger log = LoggerFactory.getLogger(PdfUtils.class);
    private static String s = "<License><Data><Products><Product>Aspose.Total for Java</Product><Product>Aspose.Words for Java</Product></Products><EditionType>Enterprise</EditionType><SubscriptionExpiry>20991231</SubscriptionExpiry><LicenseExpiry>20991231</LicenseExpiry><SerialNumber>8bfe198c-7f0c-4ef8-8ff0-acc3237bf0d7</SerialNumber></Data><Signature>sNLLKGMUdF0r8O1kKilWAGdgfs2BvJb/2Xp8p5iuDVfZXmhppo+d0Ran1P9TKdjV4ABwAgKXxJ3jcQTqE/2IRfqwnPf8itN8aFZlV3TJPYeD3yWE7IT55Gz6EijUpC7aKeoohTb4w2fpox58wWoF3SNp6sK6jDfiAUGEHYJ9pjU=</Signature></License>";

    public PdfUtils() {
    }

    public static void word2pdf(String docPath, String pdfPath) throws Exception {
        ByteArrayInputStream is = new ByteArrayInputStream(s.getBytes());
        Throwable var3 = null;

        try {
            License license = new License();
            license.setLicense(is);
            Document document = new Document(docPath);
            document.save(pdfPath, 40);
        } catch (Throwable var13) {
            var3 = var13;
            throw var13;
        } finally {
            if (is != null) {
                if (var3 != null) {
                    try {
                        is.close();
                    } catch (Throwable var12) {
                        var3.addSuppressed(var12);
                    }
                } else {
                    is.close();
                }
            }

        }

    }

    public static File mergePdf(List<File> files, String targetPath) throws IOException {
        PDFMergerUtility mergePdf = new PDFMergerUtility();
        Iterator var3 = files.iterator();

        while(var3.hasNext()) {
            File f = (File)var3.next();
            if (f.exists() && f.isFile()) {
                mergePdf.addSource(f);
            }
        }

        mergePdf.setDestinationFileName(targetPath);
        mergePdf.mergeDocuments(IOUtils.createMemoryOnlyStreamCache());
        return new File(targetPath);
    }
}
